<?php
/**
 *  * Blip.tv functions
 *
 * @copyright Almog Baku.
 * 	@author Almog Baku - almog.baku@gmail.com
 * 	@license GPLv3 non-commericial use without @author accepted.
 */

//Set timeout
set_time_limit(0);

//Include http.class
include_once("http.class.php");

class BlipAuthenticationException extends Exception{
    public function __construct($message,$code=0){
        parent::__construct($message,$code);
    }
}

class BlipDataException extends Exception{
    public function __construct($message,$code=0){
        parent::__construct($message,$code);
    }
}

class BlipPHP {

/**
 * Upload file
 * 
 * @param string[required] $username
 * @param string[required] $passoword
 * @param url[required] 	$file
 * @param string[required] $title
 * @param string[optional] 	$description
 * @return Response stdClass if succes, or FALSE if there error.
 *
 * @throws Exception if $username is blank or null
 *
 */
function upload($username=null, $passoword=null, $file=null, $title=null, $description="") {    
	if(($username==null) or ($username=="") or ($passoword==null))
    	throw new BlipAuthenticationException("AUTHENTICATION_REQUIRED: Bad login information");
	
	if(($title==null) or ($title==""))
	    throw new BlipDataException("MALFORMED TITLE: No title given.");
	
	if(($file==null))
	    throw new BlipDataException("MALFORMED FILE: No file given");
	
	//Blip.tv fields
	$data = array(
		'cmd'			=> "post",
		'section'		=> "file",
		'item_type'		=> "file",
		'post'			=> "1",
		'skin'			=> "api",
		'userlogin'		=> $username,
		'password'		=> $passoword,
		'title'			=> $title,
		'description'	=> $description
	);
	
	
	//Setting http class settings
	$http=new http_class;
	$http->timeout		= 0;
	$http->data_timeout	= 0;
	$arguments			= array();
	$response			= "";
	
	$http->GetRequestArguments("http://uploads.blip.tv/?".http_build_query($data),$arguments);
	
	$arguments["RequestMethod"]	= "POST";
	$arguments["PostValues"]	= $data;
	$arguments["PostFiles"]		= array(
		"file" => array(
			"Data"			=> file_get_contents($file),
			"Name"			=> basename($file),
			"Content-Type"	=> "Application/Octet-stream"
		)
	);
	
	//Make the request
	$http->Open($arguments);
	$http->SendRequest($arguments);
	$http->ReadReplyBody($response,1000);
	
	fclose($file);
	$xml_response = simplexml_load_string($response);
	
	if ($xml_response->status == "ERROR") {
	    if ($xml_response->error->code == "AUTHENTICATION_REQUIRED"){
	        throw new BlipAuthenticationException("AUTHENTICATION_REQUIRED: Bad login information");
	    }
	}
	
	return $xml_response;
}

/**
 * Delete item
 *
 * @param string[required] $username
 * @param string[required] $passoword
 * @param int[required] 	$id			- item id
 * @param string[required] $reason		- reason for delete
 * @param string[optional] 	$description
 * @return Response stdClass if succes, or FALSE if there error.
 */
function blip_delete($username=null, $passoword=null, $id=null, $reason="private") {
	if(($username==null) or ($passoword==null) or ($id=null))
		return false;
	
	//Blip.tv fields
	$data = array(
		'cmd'			=> "delete",
		'section'		=> "posts",
		'item_type'		=> "file",
		'post'			=> "1",
		'skin'			=> "api",
		'userlogin'		=> $username,
		'password'		=> $passoword,
		'item_id'		=> $id,
		'reason'		=> $reason
	);
	
	
	//Setting http class settings
	$http=new http_class;
	$http->timeout		= 0;
	$http->data_timeout	= 0;
	$arguments			= array();
	$response			= "";
	
	$http->GetRequestArguments("http://uploads.blip.tv/?".http_build_query($POSTData),$arguments);
	
	$arguments["RequestMethod"]	= "POST";
	$arguments["PostValues"]	= $data;
	
	//Make the request
	$http->Open($arguments);
	$http->SendRequest($arguments);
	$http->ReadReplyBody($response,1000);
	
	$xml_response = simplexml_load_string($response);
	
	if ($xml_response->status == "ERROR") {
	    throw new Exception();
	    if ($xml_response->error->code == "AUTHENTICATION_REQUIRED"){
	        throw new Exception("AUTHENTICATION_REQUIRED: Bad login information");
	    }
	}
	
	return $xml_response;
}


/**
 * Information about item
 *
 * @param int[required] $id	- item id
 * @return Response stdClass if succes, or FALSE if there error.
 */
function file_info($id) {
	return simplexml_load_file("http://www.blip.tv/file/" . $id . "?skin=api");
}
}
?>